package com.test15_异常;

import org.junit.Test;

/**
 * throw关键字
 */
public class YootkDemo_03 {

    @Test
    public void test1() throws Exception {
        // new Message().send("沐言优拓：www.yootk.com");
        new Message().send(null);
    }

    @Test
    public void test2() {
        System.out.println(echo("www.yootk.com"));
    }

    public static String echo(String message) {
        try {
            return "【TRY】" + message;
        } catch (Exception e) {
            return "【CATCH】" + message;
        } finally {
            return "【FINALLY】" + message;
        }
    }
}

class Message {
    // 消息发送失败，抛出异常
    public void send(String message) throws Exception {  //消息发送
        try {
            if (this.build()) { // 连接建立成功
                if (message == null) {
                    throw new Exception("【Exception】要发送的消息内容不允许为空！");
                }
                System.out.println("【消息发送】" + message);
                this.close();
            }
        }/* catch (Exception e) {
            throw e;
        }*/ finally {
            this.close();   // 关闭连接通道
        }
    }

    private boolean build() throws Exception {
        System.out.println("【建立连接】与网络服务器建立消息的发送通道...");
        /*if (true) {
            throw new Exception("【Exception】无法进行服务器的连接！");
        }*/
        return true;
    }

    private void close() {
        System.out.println("【关闭连接】关闭网络通道...");
    }
}
